﻿Imports lbGlobalcom
Public Class fDatosUsuarios
    Dim daCajas As dsCajaTableAdapters.CAJ_CAJASTableAdapter

    Private Sub fDatosUsuarios_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            SeC_DATOSUSUARIOSTableAdapter.Fill(DsSeguridad.SEC_DATOSUSUARIOS)
            GlO_SEDESTableAdapter.Fill(DsGlobal.GLO_SEDES)
            daCajas = New dsCajaTableAdapters.CAJ_CAJASTableAdapter
            daCajas.Fill(DsCaja.CAJ_CAJAS)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub cargaListaTiposDoc()
        lstTiposDoc.Items.Clear()
        Dim drDU As dsSeguridad.SEC_DATOSUSUARIOSRow = DsSeguridad.SEC_DATOSUSUARIOS.Rows(SEC_DATOSUSUARIOSBindingSource.Position)
        If Not drDU.IsIDLISTATIPOSDOCNull Then
            For Each TDoc As String In drDU.IDLISTATIPOSDOC.Split(",")
                lstTiposDoc.Items.Add(TDoc)
            Next
        End If
    End Sub
    Private Sub cargaTiposDocLista()
        Dim drDU As dsSeguridad.SEC_DATOSUSUARIOSRow = DsSeguridad.SEC_DATOSUSUARIOS.Rows(SEC_DATOSUSUARIOSBindingSource.Position)
        drDU.IDLISTATIPOSDOC = ""
        For Each ItemTD As String In lstTiposDoc.Items
            drDU.IDLISTATIPOSDOC &= ItemTD & ","
        Next
        If drDU.IDLISTATIPOSDOC.Trim.Length > 0 Then _
            drDU.IDLISTATIPOSDOC = drDU.IDLISTATIPOSDOC.Substring(0, drDU.IDLISTATIPOSDOC.Length - 1)
    End Sub
    Private Sub Salvar(ByVal Msj As Boolean)
        Try
            SEC_DATOSUSUARIOSBindingSource.EndEdit()
            SeC_DATOSUSUARIOSTableAdapter.Update(DsSeguridad.SEC_DATOSUSUARIOS)
            If Msj Then _
                MessageBox.Show("Se salvarón registros con exito", "Salvar Registros", MessageBoxButtons.OK)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
        Try
            Dim fLstUser As New fListaUsuariosBD
            If fLstUser.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim drDU As dsSeguridad.SEC_DATOSUSUARIOSRow = DsSeguridad.SEC_DATOSUSUARIOS.NewSEC_DATOSUSUARIOSRow
                drDU.IDUSER = fLstUser.IDUser
                DsSeguridad.SEC_DATOSUSUARIOS.Rows.Add(drDU)
                drDU = Nothing
                Salvar(False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnSalva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalva.Click
        Salvar(True)
    End Sub

    Private Sub btnElimina_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnElimina.Click
        Try
            SEC_DATOSUSUARIOSBindingSource.RemoveCurrent()
            Salvar(False)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaTDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTDoc.Click
        Try
            If DsSeguridad.SEC_DATOSUSUARIOS.Rows.Count > 0 AndAlso SEC_DATOSUSUARIOSBindingSource.Position <> -1 Then

                Dim drDU As dsSeguridad.SEC_DATOSUSUARIOSRow = DsSeguridad.SEC_DATOSUSUARIOS.Rows(SEC_DATOSUSUARIOSBindingSource.Position)
                If Not drDU.IsFILTRATIPIOSDOCNull AndAlso drDU.FILTRATIPIOSDOC = "S" Then
                    Dim fBuscaTD As New fListaTipoDocs
                    If fBuscaTD.ShowDialog = Windows.Forms.DialogResult.OK Then
                        lstTiposDoc.Items.Add(fBuscaTD.idTipoDoc)
                    End If
                    cargaTiposDocLista()
                    Salvar(False)
                Else
                    MessageBox.Show("Antes de seleccionar los tipos de docuemntos debe de activar el filtro de tipos de docuentos", "Filtro Tipos de Documentos", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub SEC_DATOSUSUARIOSBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SEC_DATOSUSUARIOSBindingSource.PositionChanged
        cargaListaTiposDoc()

    End Sub

    Private Sub EliminaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminaToolStripMenuItem.Click
        If lstTiposDoc.SelectedItems IsNot Nothing Then
            lstTiposDoc.Items.RemoveAt(lstTiposDoc.SelectedIndex)
            cargaTiposDocLista()
            Salvar(False)
        End If
    End Sub


    Private Sub lstEsCajero_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEsCajero.SelectedValueChanged
        If lstEsCajero.SelectedItem = "S" Then
            IDCAJAComboBox.Enabled = True
        Else
            IDCAJAComboBox.Enabled = False
        End If
    End Sub
End Class